package com.zlj.jdbc.pool;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

import org.junit.Test;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 * C3P0连接池
 * @author bashen
 *
 */
public class C3P0PoolDemo {
    
	/**
	 * 代码配置
	 * @throws PropertyVetoException
	 * @throws SQLException
	 */
	@Test
	public void test01() throws PropertyVetoException, SQLException {
		//创建连接池对象
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
		//对池进行四大参数的配置
		dataSource.setDriverClass("com.mysql.jdbc.Driver");
		dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/testsql");
		dataSource.setUser("root");
		dataSource.setPassword("tiger");
		//池配置
		dataSource.setMaxIdleTime(5000);
		dataSource.setMaxPoolSize(50);
		dataSource.setMinPoolSize(2);
		dataSource.setAcquireIncrement(5);
		dataSource.setInitialPoolSize(10);
		
		Connection conn = dataSource.getConnection();
		System.out.println(conn);  //com.mchange.v2.c3p0.impl.NewProxyConnection@39fb3ab6
		conn.close(); 
	}
	/**
	 * 配置文件的默认配置
	 * @throws SQLException 
	 */
	@Test
	public void test02() throws SQLException {
		/*
		 * 在创建连接池对象时,这个对象就会自动加载配置文件，不用我们来指定！
		 * 
		 * 但是前提是配置文件的名字必须为：c3p0-config.xml,并且配置文件必须放在src下
		 */
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
		Connection conn = dataSource.getConnection();
		System.out.println(conn);
		conn.close();
	}
	/**
	 * 使用命名配置信息
	 * @throws SQLException
	 */
	@Test
	public void test03() throws SQLException {
		/*
		 * 构造器的参数指定命名配置元素的名称！
		 * <named-config name="oracle-config">
		 */
		ComboPooledDataSource dataSource = new ComboPooledDataSource("oracle-config");
		Connection conn = dataSource.getConnection();
		System.out.println(conn);
		conn.close();
	}
}
